package com.example.examapp;


import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity {
    private EditText t1;
    private EditText t2;
    private Button btn1;
    private Button btn2;
    private RadioButton WrBtn;
    private RadioButton RdBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=(EditText)findViewById(R.id.editName);
        t2=(EditText)findViewById(R.id.editPassword);
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        WrBtn=(RadioButton)findViewById(R.id.radioButton);
        RdBtn=(RadioButton)findViewById(R.id.radioButton2);

        WrBtn.setOnClickListener(SaveInfo);
        RdBtn.setOnClickListener(UseSaved);


    }

    public void Indexlogin2(View view) {
        Intent intent=new Intent(MainActivity.this,Index.class);
        startActivityForResult(intent,1);
    }
    public void Index2ContainFragement(View view) {
        Intent intent=new Intent(MainActivity.this,ContainFragment.class);
        startActivityForResult(intent,1);
    }

    public void toStep(View v)
    {
        //构建AlertDialog.Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置相关参数
        builder.setTitle("消息");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("Hello,Android!");

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @SuppressLint("WrongConstant")
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "你点击了确认按钮！",3000).show();
            }
        }).setNegativeButton("取消", null)
                .setNeutralButton("其他", null);
//构建Dialog

        final AlertDialog dialog = builder.create();
        dialog.show();
//控制点击按钮不关闭
        dialog.getButton(Dialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "你选择了其他按钮",3000).show();
                dialog.dismiss();   //手动关闭
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        System.out.println(data.getStringExtra("Name"));
        if(requestCode==1&&resultCode==2)
        {
            String name=data.getStringExtra("Name");
            t1.setText(name);
        }

    }
    private View.OnClickListener SaveInfo=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent=new Intent();
                String fileName = "data.txt";                       // 文件名称
                String content = "Name"+":"+t1.getText().toString();           // 保存数据
                FileOutputStream fos;

            try {
                fos = openFileOutput(fileName, MODE_PRIVATE);
                fos.write(content.getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    };
    private View.OnClickListener UseSaved=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String content = "";
            FileInputStream fis = null;
            try {
                fis = openFileInput("data.txt");            //获得文件输入流对象
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[0];
            try {
                buffer = new byte[fis.available()];
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            content = new String(buffer);//转换成字符串
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String[] show=content.split(":");
            System.out.println(show.toString());
            t1.setText(show[1]);

        }
    };
}